﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class LogIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            DataTable dt=BussControl.RegisterControl.Instance.GetLogUserData(this.Uc_LogIn1.LogIn_UserName.Trim(),
            this.Uc_LogIn1.LogIn_Password.Trim(), this.Uc_LogIn1.LogIn_AgaInPassword.Trim());


        if (dt != null)
        {
            Session["UserName"] = dt.Rows[0]["UserName"].ToString();
            Response.Redirect(@"~/MyHomePage.aspx");
        }
        else
        {
            Response.Redirect(@"~/GetPassword.aspx");
        }

        //Response.Write(this.Uc_LogIn1.LogIn_UserName+"\t"+this.Uc_LogIn1.LogIn_Password+"\t"+this.Uc_LogIn1.LogIn_AgaInPassword);
        #region
        //DataSet ds = new DataSet();
        //ds = DataManager.RegisterManager.Instance.GetAllRegisterDataById(txt_UserName.Text, txt_Password.Text.Trim());
        //#region
        //this.TextBox1.Text = DataManager.RegisterManager.Instance.GetAllRegisterDataById(txt_UserName.Text.Trim()).ToString();
        //this.TextBox1.Text = Convert.ToString(DataManager.RegisterManager.Instance.GetAllRegisterDataById("陳宗琳"));
        //this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
        //this.TextBox2.Text = ds.Tables[0].Rows[0][1].ToString();
        //this.TextBox3.Text = ds.Tables[0].Rows.Count.ToString();

        //Label lb1 = new Label();//帳號
        //Label lb2 = new Label();//密碼
        //Label lb3 = new Label();//加密
        //Label lbp = new Label();
        //string s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("1" + "1", "sha1");
        //string s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("123" + "8395690", "sha1");
        //lbp.Text = s;
        //lb1.Text = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txt_Password.Text.Trim() + ds.Tables[0].Rows[0][2].ToString(), "sha1");
        //lb2.Text = ds.Tables[0].Rows[0][1].ToString();

        //lb1.Text = ds.Tables[0].Rows[0][0].ToString() + "\t";
        //lb2.Text = ds.Tables[0].Rows[0][1].ToString() + "\t";
        //lb3.Text = ds.Tables[0].Rows[0][2].ToString() + "\t";
        //Page.Form.Controls.Add(lb1);
        //Page.Form.Controls.Add(lb2);
        //Page.Form.Controls.Add(lb3);
        //Page.Form.Controls.Add(lbp);
        //if (ds.Tables[0].Rows.Count == 1)
        //#endregion
        //    Response.Write(ds.Tables[0].Rows[0]["Password"].ToString());
        //try
        //{
        //    if (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txt_Password.Text.Trim() + ds.Tables[0].Rows[0][3].ToString(), "sha1") == ds.Tables[0].Rows[0][2].ToString().Trim() || System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txt_Password.Text.Trim() + ds.Tables[0].Rows[0][3].ToString(), "sha1") == ds.Tables[0].Rows[0][2].ToString().Trim())
        //    {
        //        Response.Redirect(@"~/MyPage.aspx?UserName=" + ds.Tables[0].Rows[0]["UserName"].ToString());
        //    }
        //    else
        //    {
        //        if (Application["LogInCounter"] == null)
        //        {
        //            Application.Add("LogInCounter", 1);
        //        }
        //        else
        //        {
        //            Application.Set("LogInCounter", (int)Application["LogInCounter"] + 1);
        //            Label lb = new Label();
        //            lb.Text = "您已經輸入" + Application["LogInCounter"].ToString() + "次錯誤了唷!再一次就會離開此頁!";
        //            Page.Form.Controls.Add(lb);
        //            if ((int)Application["LogInCounter"] >= 3)
        //            {
        //                //Response.Write("<script>alert('" + Application["LogInCounter"] + "')</script>");
        //                Response.Redirect("GetPassword.aspx?LogInCounter=" + Application["LogInCounter"]);
        //            }
        //        }
        //    }

        //}
        //catch (Exception ex)
        //{
        //    ex.ToString();

        //}
        #region 測試
        //Response.Write(BussModel.ConfigModel.PersonialConnString.ConnectionString.ToString());
        //string sqlsel = "select * from Register where UserName='"+txt_UserName.Text.Trim()+"'  and  Password='"+txt_Password.Text.Trim()+"'";
        //string sqlcon = BussModel.Configuration.Instance.DBConntionString;
        //SqlConnection conn=new SqlConnection(sqlcon);
        //SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
        //conn.Open();
        //DataTable dt = new DataTable();
        //da.Fill(dt);
        //if (dt.Rows.Count == 1)
        //{
        //    Response.Redirect(@"~/MyPage.aspx");
        //}
        #endregion
        //if (Request.QueryString["UserName"].ToString() != null || Request.QueryString["Password"].ToString() != null)
        //{
        #region 測試
        //Label lb = new Label();
        //lb.Text=Request.QueryString["ID"].ToString();
        //Page.Form.Controls.Add(lb);
        #endregion
        //Response.Write("<script>alert('"+Request.QueryString["UserName"].ToString()+"');</script>");
        //lbl_UserName.Text = Request.QueryString["UserName"].ToString();

        #region 測試
        //DataSet ds = new DataSet();
        //ds=DataManager.RegisterManager.Instance.GetAllRegisterDataById(Request.QueryString["UserName"].ToString(), Request["Password"].ToString());
        //for (int i = 0; i <= ds.Tables[0].Rows.Count;i++ )
        //{
        //    for(int j=0;i<=ds.Tables[0].Columns.Count;j++)
        //    {
        //        Response.Write(ds.Tables[0].Rows[i][j].ToString());
        //    }
        //}

        #endregion
        #endregion
    }
}